<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Introduction to OpenCV Tracker</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Introduction to OpenCV Tracker </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>Create a tracker object.</li>
<li>Use the roiSelector function to select a ROI from a given image.</li>
<li>Track a specific region in a given image.</li>
</ul>
<h2>Source Code </h2>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4e/utility_8hpp.html">opencv2/core/utility.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../de/df6/contrib_2modules_2tracking_2include_2opencv2_2tracking_8hpp.html">opencv2/tracking.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;cstring&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv ){</div><div class="line">  <span class="comment">// show help</span></div><div class="line"><span class="comment"></span>  <span class="keywordflow">if</span>(argc&lt;2){</div><div class="line">    cout&lt;&lt;</div><div class="line">      <span class="stringliteral">&quot; Usage: tracker &lt;video_name&gt;\n&quot;</span></div><div class="line">      <span class="stringliteral">&quot; examples:\n&quot;</span></div><div class="line">      <span class="stringliteral">&quot; example_tracking_kcf Bolt/img/%04d.jpg\n&quot;</span></div><div class="line">      <span class="stringliteral">&quot; example_tracking_kcf faceocc2.webm\n&quot;</span></div><div class="line">      &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// declares all required variables</span></div><div class="line"><span class="comment"></span>  <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> roi;</div><div class="line">  <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame;</div><div class="line"></div><div class="line">  <span class="comment">// create a tracker object</span></div><div class="line"><span class="comment"></span>  <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;Tracker&gt;</a> tracker = TrackerKCF::create();</div><div class="line"></div><div class="line">  <span class="comment">// set input video</span></div><div class="line"><span class="comment"></span>  std::string video = argv[1];</div><div class="line">  <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> cap(video);</div><div class="line"></div><div class="line">  <span class="comment">// get bounding box</span></div><div class="line"><span class="comment"></span>  cap &gt;&gt; frame;</div><div class="line">  roi=<a class="code" href="../../d7/dfc/group__highgui.html#ga8daf4730d3adf7035b6de9be4c469af5">selectROI</a>(<span class="stringliteral">&quot;tracker&quot;</span>,frame);</div><div class="line"></div><div class="line">  <span class="comment">//quit if ROI was not selected</span></div><div class="line">  <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>==0 || roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>==0)</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line"></div><div class="line">  <span class="comment">// initialize the tracker</span></div><div class="line"><span class="comment"></span>  tracker-&gt;init(frame,roi);</div><div class="line"></div><div class="line">  <span class="comment">// perform the tracking process</span></div><div class="line">  printf(<span class="stringliteral">&quot;Start the tracking process, press ESC to quit.\n&quot;</span>);</div><div class="line">  <span class="keywordflow">for</span> ( ;; ){</div><div class="line">    <span class="comment">// get frame from the video</span></div><div class="line">    cap &gt;&gt; frame;</div><div class="line"></div><div class="line">    <span class="comment">// stop the program if no more images</span></div><div class="line">    <span class="keywordflow">if</span>(frame.rows==0 || frame.cols==0)</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line">    <span class="comment">// update the tracking result</span></div><div class="line"><span class="comment"></span>    tracker-&gt;update(frame,roi);</div><div class="line"></div><div class="line">    <span class="comment">// draw the tracked object</span></div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( frame, roi, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>( 255, 0, 0 ), 2, 1 );</div><div class="line"></div><div class="line">    <span class="comment">// show image with the tracked object</span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;tracker&quot;</span>,frame);</div><div class="line"></div><div class="line">    <span class="comment">//quit on ESC button</span></div><div class="line">    <span class="keywordflow">if</span>(<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(1)==27)<span class="keywordflow">break</span>;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><h2>Explanation </h2>
<ol type="1">
<li><p class="startli"><b>Set up the input video</b></p>
<div class="fragment"><div class="line">  <span class="keywordflow">if</span>(argc&lt;2){</div><div class="line">    cout&lt;&lt;</div><div class="line">      <span class="stringliteral">&quot; Usage: tracker &lt;video_name&gt;\n&quot;</span></div><div class="line">      <span class="stringliteral">&quot; examples:\n&quot;</span></div><div class="line">      <span class="stringliteral">&quot; example_tracking_kcf Bolt/img/%04d.jpg\n&quot;</span></div><div class="line">      <span class="stringliteral">&quot; example_tracking_kcf faceocc2.webm\n&quot;</span></div><div class="line">      &lt;&lt; endl;</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">  }</div></div><!-- fragment --><p> In this tutorial, you can choose between video or list of images for the program input. As written in the help, you should specify the input video as parameter of the program. If you want to use image list as input, the image list should have formatted numbering as shown in help. In the help, it means that the image files are numbered with 4 digits (e.g. the file naming will be 0001.jpg, 0002.jpg, and so on).</p>
<p class="startli">You can find video samples in opencv_extra/testdata/cv/tracking <a href="https://github.com/opencv/opencv_extra/tree/master/testdata/cv/tracking">https://github.com/opencv/opencv_extra/tree/master/testdata/cv/tracking</a></p>
</li>
<li><p class="startli"><b>Declares the required variables</b></p>
<p class="startli">You need roi to record the bounding box of the tracked object. The value stored in this variable will be updated using the tracker object.</p>
<div class="fragment"><div class="line">  <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a> roi;</div><div class="line">  Mat frame;</div></div><!-- fragment --><p> The frame variable is used to hold the image data from each frame of the input video or images list.</p>
</li>
<li><p class="startli"><b>Creating a tracker object</b></p>
<div class="fragment"><div class="line">  Ptr&lt;Tracker&gt; tracker = TrackerKCF::create();</div></div><!-- fragment --><p> There are at least 7 types of tracker algorithms that can be used:</p><ul>
<li>MIL</li>
<li>BOOSTING</li>
<li>MEDIANFLOW</li>
<li>TLD</li>
<li>KCF</li>
<li>GOTURN</li>
<li>MOSSE</li>
</ul>
<p class="startli">Each tracker algorithm has their own advantages and disadvantages, please refer the documentation of <a class="el" href="../../d0/d0a/classcv_1_1Tracker.html">cv::Tracker</a> for more detailed information.</p>
</li>
<li><p class="startli"><b>Select the tracked object</b></p>
<div class="fragment"><div class="line">  roi=<a class="code" href="../../d7/dfc/group__highgui.html#ga8daf4730d3adf7035b6de9be4c469af5">selectROI</a>(<span class="stringliteral">&quot;tracker&quot;</span>,frame);</div></div><!-- fragment --><p> Using this function, you can select the bounding box of the tracked object using a GUI. With default parameters, the selection is started from the center of the box and a middle cross will be shown.</p>
</li>
<li><p class="startli"><b>Initializing the tracker object</b></p>
<div class="fragment"><div class="line">  tracker-&gt;init(frame,roi);</div></div><!-- fragment --><p> Any tracker algorithm should be initialized with the provided image data, and an initial bounding box of the tracked object. Make sure that the bounding box is valid (size more than zero) to avoid failure of the initialization process.</p>
</li>
<li><p class="startli"><b>Update</b></p>
<div class="fragment"><div class="line">    tracker-&gt;update(frame,roi);</div></div><!-- fragment --><p> This update function will perform the tracking process and pass the result to the roi variable. </p>
</li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:39 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
